﻿using System;
using MySql.Data.MySqlClient;

namespace BingoServerCommon.Db.Pool
{
	/// <summary>
	/// 
	/// </summary>
	public class TestConnectionPool  //测试代码
	{
		public void Test()
		{
			string strcon = MysqlInit.BingoDataMysqlConnectionStr;
			ConnectionPool tmpPool = new ConnectionPool(strcon);
			tmpPool.createPool();

			DateTime beforDT = DateTime.Now;

			for (int i = 0; i < 1000; ++i)
			{
				MySqlConnection tmpConnect = tmpPool.getConnection();
				
				TestDb(tmpConnect);
				tmpPool.returnConnection(tmpConnect);//用完回收
			}

			DateTime afterDT = DateTime.Now;
			TimeSpan ts = afterDT.Subtract(beforDT);
			Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);

			tmpPool.closeConnectionPool();

			beforDT = DateTime.Now;
			for (int i = 0; i < 1000; ++i)
			{
				MySqlConnection m_sqlcon = new MySqlConnection(strcon);
				m_sqlcon.Open();
				TestDb(m_sqlcon);
				m_sqlcon.Close();
			}

			afterDT = DateTime.Now;
			TimeSpan ts3 = afterDT.Subtract(beforDT);
			Console.WriteLine("DateTime总共花费{0}ms.", ts3.TotalMilliseconds);
		}

		private void TestDb(MySqlConnection tmpConnect)
		{
			try
			{
				string sql = "SELECT count(*) FROM players;";
				MySqlCommand DBComm = new MySqlCommand(sql, tmpConnect);
				MySqlDataReader DBReader = DBComm.ExecuteReader();

				if (DBReader.Read())
				{
					//int nIndex = 0;
					//int nInsID = DBReader.GetInt32(nIndex++);
					//string strName = DBReader.GetString(nIndex++);
					//string strPassword = DBReader.GetString(nIndex++);
					//int nLoginState = DBReader.GetInt32(nIndex++);
					DBReader.Close();
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
		}
	}
}